import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { EnvEnum } from '@app/common/enum/envEnum';
import { setupSwagger } from '@app/common/plugins/setupSwagger';
import { NestExpressApplication } from '@nestjs/platform-express';
import { TransformInterceptor } from '@app/common/interceptors/transform.interceptor';
import { HttpExceptionFilter } from '@app/common/filters/http-exception.filter';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    cors: true,
  });
  // swagger 开启
  setupSwagger(app, {
    title: '全栈之巅前台API',
    description: '用于前端页面请求数据',
    version: '1.0',
    path: 'docs',
  });
  // 统一返回数据格式
  app.useGlobalInterceptors(new TransformInterceptor());
  // 全局拦截错误
  app.useGlobalFilters(new HttpExceptionFilter());
  // 验证
  app.useGlobalPipes(new ValidationPipe());

  // const PORT = process.env.NEXT_PUBLIC_ENV === 'production' ? 3008 : process.env[EnvEnum.server_port];
  const PORT = 3008;
  await app.listen(PORT);
  console.log(`nestjs server running at http://localhost:3008/docs`);
}

bootstrap();
